<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="种植时间" prop="plantTime">
        <el-date-picker clearable
          v-model="queryParams.plantTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择种植时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地块编号" prop="plotId">
        <el-select v-model="queryParams.plotId" placeholder="请选择地块号" clearable>
          <el-option
            v-for="plot in plotList"
            :key="plot.plotId"
            :label="plot.plotId"
            :value="plot.plotId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="种植品种" prop="grapeSort">
        <el-select v-model="queryParams.grapeSort" placeholder="请选择种植品种" clearable>
          <el-option
            v-for="dict in grape_plot_sort"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="种植架式" prop="plantTrellis">
        <el-select v-model="queryParams.plantTrellis" placeholder="请选择种植架式" clearable>
          <el-option
            v-for="dict in grape_plant_trells"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="种植密度" prop="plantDensity">
        <el-input
          v-model="queryParams.plantDensity"
          placeholder="请输入种植密度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行间距" prop="rowSpacing">
        <el-input
          v-model="queryParams.rowSpacing"
          placeholder="请输入行间距"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="株间距" prop="plantSpacing">
        <el-input
          v-model="queryParams.plantSpacing"
          placeholder="请输入株间距"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['grape:plant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grape:plant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grape:plant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['grape:plant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="plantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="种植编号" align="center" prop="plantId" />
      <el-table-column label="种植时间" align="center" prop="plantTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.plantTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地块编号" align="center" prop="plotId" />
      <el-table-column label="种植品种" align="center" prop="grapeSort">
        <template #default="scope">
          <dict-tag :options="grape_plot_sort" :value="scope.row.grapeSort"/>
        </template>
      </el-table-column>
      <el-table-column label="种植架式" align="center" prop="plantTrellis">
        <template #default="scope">
          <dict-tag :options="grape_plant_trells" :value="scope.row.plantTrellis"/>
        </template>
      </el-table-column>
      <el-table-column label="种植密度" align="center" prop="plantDensity" />
      <el-table-column label="行间距" align="center" prop="rowSpacing" />
      <el-table-column label="株间距" align="center" prop="plantSpacing" />
      <el-table-column label="备注" align="center" prop="plantNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['grape:plant:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grape:plant:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改种植信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="plantRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="种植时间" prop="plantTime">
          <el-date-picker clearable
            v-model="form.plantTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择种植时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地块编号" prop="plotId">
          <el-select v-model="form.plotId" placeholder="请选择地块号" clearable>
          <el-option
            v-for="plot in plotList"
            :key="plot.plotId"
            :label="plot.plotId"
            :value="plot.plotId"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="种植品种" prop="grapeSort">
          <el-select v-model="form.grapeSort" placeholder="请选择种植品种">
            <el-option
              v-for="dict in grape_plot_sort"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="种植架式" prop="plantTrellis">
          <el-select v-model="form.plantTrellis" placeholder="请选择种植架式">
            <el-option
              v-for="dict in grape_plant_trells"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="种植密度" prop="plantDensity">
          <el-input v-model="form.plantDensity" placeholder="请输入种植密度" />
        </el-form-item>
        <el-form-item label="行间距" prop="rowSpacing">
          <el-input v-model="form.rowSpacing" placeholder="请输入行间距" />
        </el-form-item>
        <el-form-item label="株间距" prop="plantSpacing">
          <el-input v-model="form.plantSpacing" placeholder="请输入株间距" />
        </el-form-item>
        <el-form-item label="备注" prop="plantNote">
          <el-input v-model="form.plantNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Plant">
import { listPlant, getPlant, delPlant, addPlant, updatePlant } from "@/api/grape/plant";

const { proxy } = getCurrentInstance();
const { grape_plot_sort, grape_plant_trells } = proxy.useDict('grape_plot_sort', 'grape_plant_trells');

const plantList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    plantTime: null,
    plotId: null,
    grapeSort: null,
    plantTrellis: null,
    plantDensity: null,
    rowSpacing: null,
    plantSpacing: null,
    plantNote: null
  },
  rules: {
    plantTime: [
      { required: true, message: "种植时间不能为空", trigger: "blur" }
    ],
    plotId: [
      { required: true, message: "地块编号不能为空", trigger: "blur" }
    ],
    grapeSort: [
      { required: true, message: "种植品种不能为空", trigger: "change" }
    ],
    plantTrellis: [
      { required: true, message: "种植架式不能为空", trigger: "change" }
    ],
    plantDensity: [
      { required: true, message: "种植密度不能为空", trigger: "blur" }
    ],
    rowSpacing: [
      { required: true, message: "行间距不能为空", trigger: "blur" }
    ],
    plantSpacing: [
      { required: true, message: "株间距不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询种植信息列表 */
function getList() {
  loading.value = true;
  listPlant(queryParams.value).then(response => {
    plantList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询地块基本信息列表,外键 */
import {listPlot} from "@/api/grape/plot";
const plotList = ref([]);
function getPlotList() {
  listPlot("").then(response => {
    plotList.value = response.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    plantId: null,
    plantTime: null,
    plotId: null,
    grapeSort: null,
    plantTrellis: null,
    plantDensity: null,
    rowSpacing: null,
    plantSpacing: null,
    plantNote: null
  };
  proxy.resetForm("plantRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.plantId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加种植信息";
  getPlotList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _plantId = row.plantId || ids.value
  getPlant(_plantId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改种植信息";
  });
  getPlotList();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["plantRef"].validate(valid => {
    if (valid) {
      if (form.value.plantId != null) {
        updatePlant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPlant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _plantIds = row.plantId || ids.value;
  proxy.$modal.confirm('是否确认删除种植信息编号为"' + _plantIds + '"的数据项？').then(function() {
    return delPlant(_plantIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('grape/plant/export', {
    ...queryParams.value
  }, `plant_${new Date().getTime()}.xlsx`)
}

getList();
getPlotList();
</script>
